package cn.tianjindong.blog_manager.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.tianjindong.blog.common.pojo.IndexPageData;
import cn.tianjindong.blog.common.utils.VisiterInfoUtils;
import cn.tianjindong.blog.mapper.BlogArticleCommentMapper;
import cn.tianjindong.blog.pojo.BlogManager;
import cn.tianjindong.blog.pojo.BlogManagerVisitlog;
import cn.tianjindong.blog_manager.service.ArticleService;
import cn.tianjindong.blog_manager.service.CommentService;
import cn.tianjindong.blog_manager.service.LinkService;
import cn.tianjindong.blog_manager.service.VisitLogService;

@Controller
public class IndexController {

	@Autowired
	private ArticleService articleService;

	@Autowired
	private LinkService linkService;

	@Autowired
	private VisitLogService visitLogService;
	
	@Autowired
	private CommentService commentService;

	@RequestMapping("/index.html")
	public String toIndexPage(HttpServletRequest request) {
		// TODO 用户评论数量，并未封装
		
		IndexPageData pageData = new IndexPageData();
		pageData.setBrowser(VisiterInfoUtils.getBrowser(request));
		pageData.setSystem(VisiterInfoUtils.getSystem(request));
		pageData.setIp(VisiterInfoUtils.getIpAddress(request));

		// 获取文章总访问数量
		Long articleVisitNum = articleService.getArticleVisitNum();
		pageData.setVisitNum(articleVisitNum);

		// 获取文章个数
		Integer articleNum = articleService.getArticleNum();
		pageData.setArticleNum(articleNum);

		// 获取友链条数
		int linkNum = linkService.getLinkNum();
		pageData.setLinkNum(linkNum);
		
		//获取评论数
		int commentNum=commentService.getCommentCount();
		pageData.setCommentNum(commentNum);

		// 获取登录的次数
		BlogManager manager=(BlogManager) request.getSession().getAttribute("user");
		int managerLoginNum = visitLogService.getManagerLoginNum(manager.getId());
		pageData.setLoginNum(managerLoginNum);
		
		//获取上一次登录记录
		BlogManagerVisitlog lastVisitLog = visitLogService.getLastVisitLog();
		request.setAttribute("lastVisitLog", lastVisitLog);
		
		request.setAttribute("pageData", pageData);
		
		return "/index";
	}

}
